package com.ben.multiple.demo.security.crypto;

import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

public class MultipleDemoSecurityCryptoApplication {

    public static void main(String[] args) {
        String data = "123456";
        String encode = springSecurityEncode(data);
        System.out.println(encode);
        boolean verify = springSecurityVerify(data, encode);
        System.out.println(verify);
    }

    /**
     * springSecurity 加密
     *
     * @param data 需要加密的数据
     * @return String
     */
    public static String springSecurityEncode(String data) {
        if (data == null) {
            return null;
        }
        PasswordEncoder delegatingPasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        return delegatingPasswordEncoder.encode(data);
    }

    /**
     * springSecurity 密码验证
     *
     * @param rawPassword  原始密码
     * @param encodeResult 加密密码
     * @return boolean
     */
    public static boolean springSecurityVerify(String rawPassword, String encodeResult) {
        if (rawPassword == null || encodeResult == null || "".equals(rawPassword) || "".equals(encodeResult)) {
            return false;
        }
        PasswordEncoder delegatingPasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        return delegatingPasswordEncoder.matches(rawPassword, encodeResult);
    }
}
